<template>
  <UploadExcelComponent :on-success="handleSuccess" />
</template>

<script>
import { reqImportEmployee } from '@/api/employees'
import { formatExcelDate } from '@/utils'
export default {
  name: 'Import',
  methods: {
    async handleSuccess({ header, results }) {
      // header中, results中的数据是中文的, 但是提交给后台的要是英文的
      if (this.$route.query.type === 'employees') {
        const userRelations = {
          '入职日期': 'timeOfEntry',
          '手机号': 'mobile',
          '姓名': 'username',
          '转正日期': 'correctionTime',
          '工号': 'workNumber'
        }
        //   把数组对象中的键值改为英文的
        const uploadData = results.map(item => {
          const obj = {}
          for (const key in item) {
            if (['correctionTime', 'timeOfEntry'].includes(userRelations[key])) {
              obj[userRelations[key]] = formatExcelDate(item[key], '-')
            } else {
              obj[userRelations[key]] = item[key]
            }
          }
          return obj
        })
        await reqImportEmployee(uploadData)
        this.$router.back()
      }
    }
  }
}
// correctionTime
// timeOfEntry
</script>

<style>

</style>
